package com.zhangsun.newMvvm.view;

import android.arch.lifecycle.ViewModelProviders;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.zhangsun.newMvvm.R;
import com.zhangsun.newMvvm.adapter.UserAdapter;
import com.zhangsun.newMvvm.base.bean.ResponseData;
import com.zhangsun.newMvvm.base.enums.PagingLoadTypeEnum;
import com.zhangsun.newMvvm.base.view.BaseActivity;
import com.zhangsun.newMvvm.common.tools.CollectionUtil;
import com.zhangsun.newMvvm.databinding.MainActivityBinding;
import com.zhangsun.newMvvm.handler.MainEventHandlers;
import com.zhangsun.newMvvm.model.bean.UserPageBean;
import com.zhangsun.newMvvm.viewModel.MainViewModel;

public class MainActivity extends BaseActivity<MainActivityBinding, MainViewModel> {

    private UserAdapter userAdapter;
    private RefreshLayout refreshLayout;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public MainViewModel initViewModel() {
        return ViewModelProviders.of(this).get(MainViewModel.class);
    }

    @Override
    protected void initView() {
        binding.setEventHandler(new MainEventHandlers(this));

        RecyclerView recyclerView = binding.userRecyclerView;
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        userAdapter = new UserAdapter(this);
        recyclerView.setAdapter(userAdapter);

        refreshLayout = binding.refreshLayout;
        setRefreshLayoutListener();
    }

    private void setRefreshLayoutListener() {
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            refreshLayout.resetNoMoreData();
            UserPageBean userPage = new UserPageBean();
            userPage.setPage(true);
            userPage.setCurrent(1);
            loadData(PagingLoadTypeEnum.REFRESH, userPage);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            UserPageBean userPage = new UserPageBean();
            userPage.setPage(true);
            userPage.setCurrent(page.getCurrent() + 1);
            loadData(PagingLoadTypeEnum.MORE, userPage);
        });
    }

    @Override
    protected void initData() {
        viewModel.getUserData().observe(this, responseData -> {
            if (responseData != null && responseData.getData() != null
                    && CollectionUtil.isNotEmpty(responseData.getData().getRecords())) {
                PagingLoadTypeEnum pagingLoadTypeEnum = responseData.getPagingLoadTypeEnum();
                userAdapter.setUsers(pagingLoadTypeEnum, responseData.getData().getRecords());
                page.setTotal(responseData.getData().getTotal());
                page.setCurrent(responseData.getData().getCurrent());
            }
        });

        showDialog("正在加载");
        UserPageBean userPage = new UserPageBean();
        userPage.setPage(true);
        userPage.setCurrent(1);
        loadData(PagingLoadTypeEnum.FIRST, userPage);
    }

    private void loadData(PagingLoadTypeEnum pagingLoadTypeEnum, UserPageBean userPage) {
        viewModel.getUserList(pagingLoadTypeEnum, userPage);
    }

    @Override
    protected void finishByPagingLoadType(PagingLoadTypeEnum pagingLoadTypeEnum) {
        switch (pagingLoadTypeEnum) {
            case FIRST:
                dismissDialog();
                break;
            case REFRESH:
                refreshLayout.finishRefresh();
                break;
            case MORE:
                refreshLayout.finishLoadMore();
                break;
            case MORE_NO_MORE:
                refreshLayout.finishLoadMoreWithNoMoreData();
                break;
            default:
                break;
        }
    }

}
